<?php

    /**
     * @package      Modules
     * @subpackage   Widgets_Setup
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
     class Widgets_Setup_Installer extends Modules_Setup_Installer
     {

    ////////////////////////////////////////////////////////////////////////////
    //                               Attributes                               //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Show module
         *
         * @var     string
         */
        protected $_display = false;

    ////////////////////////////////////////////////////////////////////////////
    //                                Methods                                 //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Pre install
         *
         * Warning : launched each time parameter "generate" is set to true, even if the module
         * is already installed
         *
         * @throws       Core_Exception
         * @return       void
         */
        public function preInstall()
        {
            $registry = Zend_Registry::getInstance();

            foreach ($registry['orderedModules'] as $module) {
                /*
                 * List all widget files
                 */
                $widgetsPath = Core_IO_Path::combine($registry['modules'][$module], 'Classes', 'Widgets');

                if (!Core_IO_Directory::exists($widgetsPath)) {
                    continue;
                }

                $widgets = Core_IO_Directory::getFiles($widgetsPath, false, false);

                foreach ($widgets as $widget) {
                    $className = $module .'_Classes_Widgets_'. Core_IO_Path::getFileNameWithoutExtension($widget);

                    if (class_exists($className, true)) {
                        $oWidget = new $className();

                        if (!$oWidget instanceof Widgets_Classes_Widget) {
                            continue;
                        }

                        /*
                         * Checks if widget is already registred
                         */
                        $dbWidget   = Doctrine_Query::create()
                                        ->from('DbWidget w')
                                        ->where('w.class_name = ?', $className)
                                        ->fetchOne();

                        if ($dbWidget) {
                            continue;
                        }

                        /*
                         * Register new widget
                         */
                        $dbWidget               = new DbWidget();
                        $dbWidget->status       = DbBase::DISABLED;
                        $dbWidget->class_name   = $className;
                        $dbWidget->name         = $oWidget->getName();
                        $dbWidget->save();

                        foreach ($oWidget->getParams() as $widgetKey => $widgetParam) {
                            /*
                             * Register widget params
                             */
                            if (!isset($widgetParam['type']) || !isset($widgetParam['default']) || !isset($widgetParam['name'])) {
                                continue;
                            }

                            $dbWidgetParam                  = new DbWidgetParam();
                            $dbWidgetParam->status          = DbBase::ENABLED;
                            $dbWidgetParam->param_key       = $widgetKey;
                            $dbWidgetParam->param_type      = $widgetParam['type'];
                            $dbWidgetParam->param_name      = $widgetParam['name'];
                            $dbWidgetParam->default_value   = $widgetParam['default'];
                            $dbWidgetParam->DbWidget        = $dbWidget;
                            $dbWidgetParam->save();
                        }
                    }
                }
            }
        }
     }
